# coding=utf-8
# 全世界最帅的帅哥
from datetime import datetime

from flask import g
from sqlalchemy import event
from sqlalchemy.orm import mapper

from ysgg1_club_comment.modules import db


def get_current_time():
    # 这里可以替换为您自己的时间获取逻辑
    return datetime.now()


class BaseModel(db.Model):
    # 抽象基类
    __abstract__ = True

    id = db.Column(db.BIGINT, autoincrement=True, primary_key=True, comment="主键")
    create_by = db.Column(db.String(32), comment="创建人")
    update_by = db.Column(db.String(32), comment="更新人")
    create_time = db.Column(db.DateTime, default=datetime.now(), comment="创建时间")
    update_time = db.Column(db.DateTime, default=datetime.now(), onupdate=get_current_time, comment="更新时间")
    is_delete = db.Column(db.Boolean, default=False, comment="是否删除")


@event.listens_for(mapper, 'before_insert')
def before_insert_listener(mapper_, connection, target):
    token = g.token
    target.create_by = token


@event.listens_for(mapper, 'before_update')
def before_insert_listener(mapper_, connection, target):
    token = g.token
    target.create_by = token
